package cn.sys.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtil {
    private SqlSession sqlSession;
    /**
     * 获取SqlSession对象
     */
    public static SqlSession getSQLSession(){
        //1. 加载配置文件
        InputStream inputStream = MybatisUtil.class.getClassLoader().getResourceAsStream("mybatis-config.xml");

        //2. 创建SqlSessionFactory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //3. 创建SqlSession，并制指定事务自动提交
        SqlSession sqlSession = sessionFactory.openSession(true);

        return sqlSession;
    }

    /**
     * 关闭sqlSession连接
     */
    public static void closeSqlSession(SqlSession sqlSession){
        sqlSession.close();
    }

}
